package com.first.wchat.widget;

import java.util.ArrayList;

import com.first.wchat.data.MSGData;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ChatListAdapter extends BaseAdapter{

	ArrayList<MSGData> mList;
	LayoutInflater inflater;
	public ChatListAdapter(Context ctx)
	{
		inflater = LayoutInflater.from(ctx);
	}
	
	
	public void setData(ArrayList<MSGData> list)
	{
		mList = list;
	}
	
	public void add(MSGData data)
	{
		mList.add(data);
	}
	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {

		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {


		Holder holder = null;
		if(convertView == null)
		{
			convertView = inflater.inflate(com.first.wchat.R.layout.chat_item, null);
			holder = new Holder();
			holder.layout =  convertView;
			convertView.setTag(holder);
		}
		else
		{
			holder = (Holder) convertView.getTag();
		}

		TextView name = (TextView)holder.layout.findViewById(com.first.wchat.R.id.name);
		name.setText(mList.get(position).getName());
		
		TextView msg = (TextView)holder.layout.findViewById(com.first.wchat.R.id.msg);
		msg.setText(mList.get(position).getMsg());
		
		return convertView;
	
	}
	
	class Holder
	{
		View layout;
	}

}
